<?php
include_once('./config/init.php');


//判断是否有登录
AuthLogin();


$action = isset($_POST['action']) ? $_POST['action'] : '';

//如果action == 'check' 我就知道他的请求是 ajax 异步请求过来的
if ($action == "check") {
    $sql = "SELECT dep.*,job.depid FROM {$pre_}department AS dep LEFT JOIN {$pre_}job AS job ON dep.id = job.depid";
    // $sql = "SELECT * FROM {$pre_}department";
    $list = all($sql);


    foreach ($list as $item) {
        $name = [
            'value' => 1,
            'name' => $item['name']
        ];
        $data[] = $name;
    }
    echo json_encode($data);
    exit;
}
?>



<!DOCTYPE html>
<html lang="en">

<head>
    <?php include_once('meta.php'); ?>
    <script src="./assets/lib/echarts.js"></script>
</head>

<body>
    <?php include_once('header.php'); ?>

    <?php include_once('menu.php'); ?>

    <div class="content">
        <div class="header">
            <h1 class="page-title">后台首页</h1>
        </div>
        <ul class="breadcrumb">
            <li><a href="index.html">控制台</a> <span class="divider">/</span></li>
            <li class="active">首页</li>
        </ul>
        <div id="container" style="width: auto; height:100vh;"></div>

        <div class="container-fluid">
            <div class="row-fluid">
                <footer>
                    <hr>
                    <p>&copy; 2017 <a href="#" target="_blank">copyright</a></p>
                </footer>
            </div>
        </div>
    </div>
</body>

</html>
<script type="text/javascript">
    // 基于准备好的dom，初始化echarts实例
    // var myChart = echarts.init(document.getElementById('main'));
    var dom = document.getElementById('container')
    var MyChart = echarts.init(dom)


    var option = {
        title: {
            text: '部门下的职位数量',
            subtext: '纯属虚构',
            left: 'center',
        },

        tooltip: {
            trigger: 'item',
        },

        legend: {
            orient: 'vertical',
            left: 'left',
        },

        series: [{
            //饼图
            type: 'pie',
            name: '访问来源', //分类标记
            radius: '50%', //半径
            data: [],
            //高亮
            emphasis: {
                //jquery
                itemStyle: {
                    shadowBlur: 10, // 阴影模糊
                    shadowOffsetX: 0, //X轴偏移
                    shadowColor: 'rgba(0, 0, 0, 0.5)', //模糊颜色
                }
            }
        }]
    }

    var names = [];

    $.ajax({
        type: 'post',
        url: 'index.php',
        data: {
            action: 'check',
        },
        dataType: 'json',
        success: function(result) {
            if (result) {
                for (var i = 0; i < result.length; i++) {
                    // if(result[i].name == result[i-1].name){
                    //     console.log(123)
                    // }
                    names.push(result[i])
                    console.log(result[i].value)
                }
                MyChart.setOption({
                    series: [{
                        data: names
                    }]
                })

            }
        }
    })




    MyChart.setOption(option)
</script>